package com.example.service;

import com.example.pojo.Order;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @program: springboot_rabbitmq
 * @description: 消息接收
 * @author: ChenZhiXiang
 * @create: 2019-06-04 17:46
 **/
@Component
public class OrderReceiver {

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "queues-order",durable = "true"),
            exchange = @Exchange(value = "exchange-order",durable = "true",type = "topic"),
            key = "routingKey-order"
            )
    )
    // 手动签收依赖channel
    @RabbitHandler
    public void onOrderMessage(@Payload Order order, @Headers Map<String,Object> map, Channel channel) throws Exception{
        System.out.println("-----------------------------接收到消息------------------------");
        System.out.println(order.getId());
        Long deliveryTag = (Long) map.get(AmqpHeaders.DELIVERY_TAG);
        // 因为是手动签收，要返回一个标识给rabbitmq服务，让服务端知道它已经消费，可以删除了
        channel.basicAck(deliveryTag,false);
    }

}
